package san.web.system;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import com.security.domain.Resource;
import com.utils.Page;
import com.web.struts2.CRUDSupportAction;

/**
 * 资源管理Action.
 * 
 * @see CRUDActionSupport
 * @author san
 */
@ParentPackage("default")
@Results( { @Result(name = "reload", location = "resource.shtml", type = "redirect") })
public class ResourceAction extends CRUDSupportAction {

	private static final long serialVersionUID = 8517636900960039101L;

	private Resource resource;

	private Page<Resource> page = new Page<Resource>(10);// 每页10条记录

	@Override
	protected void prepareModel() throws Exception {
		if (resource != null && StringUtils.isNotBlank(resource.getId())) {
			resource = userService.getResource(resource.getId());
		} else {
			if (StringUtils.isNotBlank(getId())) {
				resource = userService.getResource(getId());
			} else {
				resource = new Resource();
			}
		}
	}

	@Override
	public String list() throws Exception {
		page = userService.getAllResources(page);
		return SUCCESS;
	}

	@Override
	public String input() throws Exception {
		if (StringUtils.isNotBlank(getId())) {
			resource = userService.getResource(getId());
		} else {
			resource = new Resource();
		}
		return INPUT;
	}

	@SuppressWarnings("finally")
	@Override
	public String save() throws Exception {
		try {
			userService.saveResource(resource);
			addActionMessage("保存资源成功!");
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			addActionError("保存资源失败!");
		} finally {
			return RELOAD;
		}
	}

	@SuppressWarnings("finally")
	@Override
	public String delete() throws Exception {
		try {
			userService.deleteResource(getId());
			addActionMessage("删除资源成功!");
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			addActionError("删除资源失败!");
		} finally {
			return RELOAD;
		}
	}

	public Resource getResource() {
		return resource;
	}

	public void setResource(Resource resource) {
		this.resource = resource;
	}

	public Page<Resource> getPage() {
		return page;
	}

	public void setPage(Page<Resource> page) {
		this.page = page;
	}
}